package com.tianyang.yukuaifu.module.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.business.trade.bean.TradeFilterInput;
import com.tianyang.yukuaifu.common.utils.DateCheckPopListener;
import com.tianyang.yukuaifu.module.dialog.interfaces.ICallbackListener;
import com.tianyang.yukuaifu.module.roundview.RoundTextView;
import com.tianyang.yukuaifu.utils.DateUtils;
import com.tianyang.yukuaifu.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：交易筛选
 * Created by xyj on 2024/3/11
 */
public class TradeFilterDialog extends Dialog {

    @BindView(R.id.startTimeRtv)
    RoundTextView startTimeRtv;
    @BindView(R.id.endTimeRtv)
    RoundTextView endTimeRtv;
    @BindView(R.id.payTypeWxRtv)
    RoundTextView payTypeWxRtv;
    @BindView(R.id.payTypeAliRtv)
    RoundTextView payTypeAliRtv;
    @BindView(R.id.payTypeUnionRtv)
    RoundTextView payTypeUnionRtv;
    @BindView(R.id.payTypeSxyRtv)
    RoundTextView payTypeSxyRtv;
    @BindView(R.id.paySuccessRtv)
    RoundTextView paySuccessRtv;
    @BindView(R.id.payFailRtv)
    RoundTextView payFailRtv;
    @BindView(R.id.refundRtv)
    RoundTextView refundRtv;

    private Context mContext;

    private DateCheckPopListener mDateStartPopListener;

    private DateCheckPopListener mDateEndPopListener;

    private TradeFilterInput mInput;

    private ICallbackListener<TradeFilterInput> listener;

    private int currentType = 0, currentStatus = -1;

    private String startTime, endTime;

    public void setListener(ICallbackListener<TradeFilterInput> listener) {
        this.listener = listener;
    }

    public TradeFilterDialog(Context context) {
        super(context, R.style.top_pop_menu);
        this.mContext = context;
        setContentView(R.layout.dialog_trade_filter);
        ButterKnife.bind(this);
        startTime = DateUtils.getTimeAgo(30, "yyyy-MM-dd");
        endTime = DateUtils.getCurrentTime("yyyy-MM-dd");
        startTimeRtv.setText(startTime);
        endTimeRtv.setText(endTime);
    }

    @OnClick({R.id.confirmRtv, R.id.resetRtv, R.id.startTimeRtv, R.id.endTimeRtv, R.id.payTypeWxRtv,
            R.id.payTypeAliRtv, R.id.payTypeUnionRtv, R.id.payTypeSxyRtv, R.id.paySuccessRtv, R.id.payFailRtv, R.id.refundRtv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.confirmRtv:
                String startDateStr = startTimeRtv.getText().toString();
                String endDateStr = endTimeRtv.getText().toString();
                if (DateUtils.getDifferenceTime(startDateStr, endDateStr) < 0) {
                    ToastUtil.show("查询结束日期必须大于开始日期");
                    return;
                }
                mInput = new TradeFilterInput();
                if (!TextUtils.isEmpty(startDateStr))
                    mInput.setStartTime(startDateStr);
                if (!TextUtils.isEmpty(endDateStr))
                    mInput.setEndTime(endDateStr);
                if (currentType != 0)
                    mInput.setType(currentType + "");
                if (currentStatus != -1)
                    mInput.setOrderStatus(currentStatus + "");
                if(listener != null) {
                    listener.callback(mInput);
                }
                dismiss();
                break;
            case R.id.resetRtv:
                startTimeRtv.setText(startTime);
                endTimeRtv.setText(endTime);
                showPayType(0);
                showPayStatus(-1);
                mInput = null;
                if(listener != null) {
                    listener.callback(null);
                }
                dismiss();
                break;
            case R.id.startTimeRtv:
                if (mDateStartPopListener == null) {
                    mDateStartPopListener = new DateCheckPopListener((Activity) mContext, true);
                }
                mDateStartPopListener.handleRegionCheck(view, "请选择开始日期", startTimeRtv);
                break;
            case R.id.endTimeRtv:
                if (mDateEndPopListener == null) {
                    mDateEndPopListener = new DateCheckPopListener((Activity) mContext, true);
                }
                mDateStartPopListener.handleRegionCheck(view, "请选择结束日期", endTimeRtv);
                break;
            case R.id.payTypeWxRtv:
                showPayType(2);
                break;
            case R.id.payTypeAliRtv:
                showPayType(3);
                break;
            case R.id.payTypeUnionRtv:
                showPayType(4);
                break;
            case R.id.payTypeSxyRtv:
                showPayType(1);
                break;
            case R.id.paySuccessRtv:
                showPayStatus(0);
                break;
            case R.id.payFailRtv:
                showPayStatus(3);
                break;
            case R.id.refundRtv:
                showPayStatus(5);
                break;
        }
    }

    private void showPayStatus(int index) {
        if (index != currentStatus) {
            currentStatus = index;
        } else {
            currentStatus = -1;
        }
        paySuccessRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.c_f8f8f8));
        paySuccessRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_333333));
        payFailRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.c_f8f8f8));
        payFailRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_333333));
        refundRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.c_f8f8f8));
        refundRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_333333));
        switch (currentStatus) {
            case 0:
                paySuccessRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                paySuccessRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                break;
            case 3:
                payFailRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                payFailRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                break;
            case 5:
                refundRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                refundRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                break;
        }
    }

    private void showPayType(int index) {
        if (index != currentType) {
            currentType = index;
        } else {
            currentType = 0;
        }
        payTypeWxRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.c_f8f8f8));
        payTypeWxRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_333333));
        payTypeAliRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.c_f8f8f8));
        payTypeAliRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_333333));
        payTypeUnionRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.c_f8f8f8));
        payTypeUnionRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_333333));
        payTypeSxyRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.c_f8f8f8));
        payTypeSxyRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_333333));
        switch (currentType) {
            case 1:
                payTypeSxyRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                payTypeSxyRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                break;
            case 2:
                payTypeWxRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                payTypeWxRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                break;
            case 3:
                payTypeAliRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                payTypeAliRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                break;
            case 4:
                payTypeUnionRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                payTypeUnionRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                break;
        }
    }
}
